package com.chenl.provider.DEMOS.接入第三方.DingDing;


import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSONObject;
import com.chenl.provider.DEMOS.接入第三方.DingDing.Dto.DingMsg;
import com.chenl.provider.DEMOS.接入第三方.DingDing.Dto.DingMsgDto;
import com.chenl.provider.util.HttpUtils;
import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.net.URLEncoder;


//https://open.dingtalk.com/document/resourcedownload/download-server-sdk
@Component
public class SendDingUtil {
    @Autowired
    private RestTemplate restTemplate;

    @Value("${dingding.secret:SEC228b6098b42ae95896365f42f259b53ea432ec72a45fe3616e7b77f884dfbf9c}")
    private String secret;

    public static void main(String[] args) {
        DingMsgDto dingMsgDto = new DingMsgDto();
        DingMsg dingMsg = new DingMsg();
        dingMsg.setContent("测试消息看看");
        dingMsgDto.setMsgtype("text");
        dingMsgDto.setText(dingMsg);
//        System.out.println(JSONObject.toJSONString(dingMsgDto));
        System.out.println(JSONObject.toJSONString(dingMsgDto));
        String post = HttpUtil.post("https://oapi.dingtalk.com/robot/send?access_token=3003d76000e56b78b1a3e06fe9797620785216be4c21a95b2a5613943a65a1e5", JSONObject.toJSONString(dingMsgDto));
        System.out.println(post);
    }
    public  String sendDingMsg(String msg) throws Exception {
        DingMsgDto dingMsgDto = new DingMsgDto();
        DingMsg dingMsg = new DingMsg();
        dingMsg.setContent(msg);
        dingMsgDto.setMsgtype("text");
        dingMsgDto.setText(dingMsg);
        Long timestamp = System.currentTimeMillis();
        String sign = generateSign(timestamp);
        String url="https://oapi.dingtalk.com/robot/send?access_token=3003d76000e56b78b1a3e06fe9797620785216be4c21a95b2a5613943a65a1e5&timestamp="
                +timestamp+"&sign="+sign;
        HttpUtils.HttpResult httpResult = HttpUtils.PostHttp(url, JSONObject.toJSONString(dingMsgDto), null, restTemplate, String.class);
        return httpResult.toString();
    }

    public  String sendDingMsg2(String msg) throws Exception {
        DingMsgDto dingMsgDto = new DingMsgDto();
        DingMsg dingMsg = new DingMsg();
        dingMsg.setContent(msg);
        dingMsgDto.setMsgtype("text");
        dingMsgDto.setText(dingMsg);
        Long timestamp = System.currentTimeMillis();
        String sign = generateSign(timestamp);
        String url="https://oapi.dingtalk.com/robot/send?access_token=3003d76000e56b78b1a3e06fe9797620785216be4c21a95b2a5613943a65a1e5&timestamp="
                +timestamp+"&sign="+sign;
        String res = HttpUtil.post(url, JSONObject.toJSONString(dingMsgDto));
        return res;
    }

    public  String generateSign(Long timestamp) throws Exception{
        String stringToSign = timestamp + "\n" + secret;
        Mac mac = Mac.getInstance("HmacSHA256");
        mac.init(new SecretKeySpec(secret.getBytes("UTF-8"), "HmacSHA256"));
        byte[] signData = mac.doFinal(stringToSign.getBytes("UTF-8"));
        String sign = URLEncoder.encode(new String(Base64.encodeBase64(signData)),"UTF-8");
        System.out.println(sign);
        return sign;
    }
}
